package com.boomsecret.juc.executor;

import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/21.
 */
public class TimeUnitDemo {

    public static void main(String[] args) throws Exception {
        System.out.println(TimeUnit.valueOf("DAYS"));
        System.out.println("-----------------");

        for (TimeUnit c : TimeUnit.values()) {
            System.out.println(c);
        }
        System.out.println("-----------------");

        System.out.println(TimeUnit.SECONDS.convert(1L, TimeUnit.DAYS));
        System.out.println("-----------------");

        System.out.println(TimeUnit.MINUTES.toSeconds(1L));
        System.out.println("-----------------");

        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread end.");
        });
        thread.start();
        // join 过了1s就不等待了
        TimeUnit.SECONDS.timedJoin(thread, 1);
        System.out.println("-----------------");

        testTimeUnitWait();
        System.out.println("-----------------");
    }

    private static void testTimeUnitWait() throws InterruptedException {
        Object object = new Object();
        synchronized (object) {
            System.out.println("testTimeUnitWait start");
            // 直接阻塞当前线程 等待5s就不等待了
            TimeUnit.SECONDS.timedWait(object, 5);
            System.out.println("testTimeUnitWait end");
        }
    }
}
